<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Cache;
use app\admin\model\Files;
use think\facade\Filesystem;
use think\exception\ValidateException;


class Index extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }
    public function index()
    {
        if($modname=input('modname')){
            $this->site['modinfo']=\app\admin\model\Mod::where('name',$modname)->find();
        }
        //总营业额-订单总金额
        $order_total_money = \app\admin\model\Order::sum('money');
        //订单总量
        $order_total_count = \app\admin\model\Order::count();
        //客户总量-公司总量
        $company_total_count = \app\admin\model\Company::count();
        //业务总量-业务员数量
        $salesman_total_count = \app\admin\model\Admin::hasWhere('access',['roles_id'=>5])->count();
        //获取公告数据
        $article_list = \app\admin\model\Article::order('recommends desc,update_time desc')->append(['recommends_name','admin_id_name'])->limit(0,10)->select()->toArray();
        $index_data = [
            'order_total_money'=>$order_total_money,
            'order_total_count'=>$order_total_count,
            'company_total_count'=>$company_total_count,
            'salesman_total_count'=>$salesman_total_count,
            'article_list'=>$article_list,
        ];
        $this->site['index_data'] = $index_data;
        //折线图数据
        $month_time = getMonth();
        $month_data  = [];
        $company_total_count_data = [];
        $order_total_money_data = [];
        foreach ($month_time as $month_value){
            $month_data[] = '第'.date('m',$month_value['start_time']).'月';
            $map = [];
            $map[]  = [
                'create_time','between',[$month_value['start_time'],$month_value['end_time']]
            ];
            //公司数量
            $company_total_count_data[] = \app\admin\model\Company::where($map)->count();
            //总营业额
            $order_total_money_data[] = \app\admin\model\Order::where($map)->sum('money');
        }
        $chart_value_data = [];
        $chart_value_data[] = [
            'name'=>'总营业额',
            'value'=>$order_total_money_data
        ];
        $chart_value_data[] = [
            'name'=>'客户总量',
            'value'=>$company_total_count_data
        ];
        $line_name = ['总营业额','客户总量'];
        $chart_data = [
            'title'=>date('Y').'年销售统计折线图',
            'line_name'=>$line_name,
            'month_data'=>$month_data,
            'value_data'=>$chart_value_data,
        ];
        $this->site['index_data']['chart_data'] = getBrokenLineChart($chart_data);
        //公司
        //总营业额
        return $this->success('基础数据获取成功', $this->site);
    }
    public function clearCache()
    {
        Cache::clear();
        return $this->success('清除缓存成功');
    }

    public function upload($id = 0)
    {
        $file = $this->request->file('file') ? $this->request->file('file') : $this->request->file('upload');
        $from = ''; //来源
        if ($this->request->file('file')) {
            $from = 'yp';
        }
        if ($this->request->file('upload')) {
            $from = 'ck';
        }
        if (!$file) {
            return 'no files';
        }
    
        $fileSize = $this->site['uploadsize'] * 1024 * 1000;
        $fileExt = $this->site['filetype'];
        $filesDir = $this->site['filesdir'];
        $checkFile = ['files' => ['filesize' => $fileSize, 'fileExt' => $fileExt]];
        try {
            validate($checkFile)->check(['files' => $file]);

            if(strstr($file->getMime(),'video')){
                $filesDir='video';
            }
            
            $savename = Filesystem::disk('public')->putFile($filesDir, $file);

            $path = Filesystem::getDiskConfig('public', 'url') . '/' . str_replace('\\', '/', $savename);
          
            //附件入库
            $data['name'] = $file->getOriginalName();
            $data['filepath'] = $path;
            $data['addtime'] = time();
            $data['ftype'] = $file->getMime();
            $data['fsize'] = $file->getSize();
            $data['user_id'] = 0;
            $data['admin_id'] = $this->admin->id;
            $data['ypcms_id'] = 0;
            $data['category_id']=0;
            $data['isq'] = 0;
            if (isset($this->user->userpic->id)) {
                Files::destroy($this->user->userpic->id);
                $r = Files::create($data);
                $fileId = $r->id;
            } else {
                $r = Files::create($data);
                $fileId = $r->id;
            }

            $result = [
                'uploaded' => 1,
                'url' =>  '//' . $this->site['siteurl'] . $data['filepath'],
                'fileName' => $data['name'],
                'fileId' => $fileId,
            ];
            if ($from == 'yp') {
                $this->success('上传成功', $result);
            } else {
                return json_encode(['url' => $result['url']]);
            }
        } catch (ValidateException $e) {

            $result = [
                'uploaded' => 0,
                'error' => ["message" => $e->getError()],
            ];

            return $this->error($e->getError());
        }
    }




}
